if exists (select 1
          from sysobjects
          where  id = object_id('bd_proc_ot_detail_del')
          and type in ('P','PC'))
   drop procedure bd_proc_ot_detail_del
go


create procedure bd_proc_ot_detail_del (@order_id varchar(200),@user_serial_str varchar(max),@ip varchar(200),@operator varchar(200),@reason varchar(max)) as
BEGIN
	--说明：加班取消分两种：整单取消、明细取消；(区分：以@user_serial_str是否为null为标准，为null则整单取消，反之明细取消)
				--整单取消：整理需要取消的明细信息时，需要过滤之前明细取消过的记录；，修改主单标识，并保存取消信息
				--明细取消：只整理该单的这些人的明细信息。修改明细标识，并保存取消信息

-- 关闭打印受影响行数
SET NOCOUNT ON;

	--  异常扑捉机制
		BEGIN TRY

			CREATE TABLE #order_detail_info(
				id INT IDENTITY,
				order_id int,
				user_serial varchar(50) COLLATE Chinese_PRC_CI_AS,
				date date
			)


		-- 开启事务
			BEGIN TRAN

        -- 如果用户编号不为空 封装处理数据(说明是明细取消)
				if(isnull(@user_serial_str,'') <> '')
				BEGIN
					insert into #order_detail_info(order_id,user_serial)
					select @order_id,a.value
					from string_split(@user_serial_str,',') a
				END
        else BEGIN
				  -- 如果用户编号为空 封装处理数据
					insert into #order_detail_info(order_id,user_serial)
					select @order_id,a.user_serial
					from lr_jiaba a
					where a.order_id = @order_id and a.is_delete !=1
				END

        -- 更新加班详情单参数
        update a set a.date = convert(varchar(10),b.jbrq,120)
        from #order_detail_info a
        inner join lr_jiaba b on a.order_id = b.order_id and a.user_serial = b.user_serial

				-- 如果用户编号不为空 封装处理数据(说明是明细取消)
				if(isnull(@user_serial_str,'') <> '')
				BEGIN
					-- 逻辑删除加班详情单
					update a set a.is_delete = 1,
										a.remove_user = @operator,
										a.remove_time = getDate(),
										a.return_remark = @reason
					from lr_jiaba a
					inner join #order_detail_info b on a.order_id = b.order_id and a.user_serial = b.user_serial;


					--校验，若全部子单都is_delete = 1 则将主单也一并取消掉

					DECLARE @notLeaveCount int = 0

					select @notLeaveCount = count(1) from lr_jiaba where order_id = @order_id and is_delete = 0

					if @notLeaveCount = 0
						BEGIN
							--逻辑删除加班单
							update a set a.is_delete = 1,
												a.remove_user = @operator,
												a.remove_time = getDate(),
												a.return_remark = '子单全部取消'
							from bd_over_time_order a
							where a.order_id = @order_id;
						END
				END
				--整单取消
				ELSE BEGIN
					-- 取消加班详情单
					update a set a.is_delete = 1
					from lr_jiaba a
					inner join #order_detail_info b on a.order_id = b.order_id and a.user_serial = b.user_serial;

					--取消加班单
					update a set a.is_delete = 1,
										a.remove_user = @operator,
										a.remove_time = getDate(),
										a.return_remark = @reason
					from bd_over_time_order a
					where a.order_id = @order_id;


					--处理排班明细数据
					DECLARE @sourceKey int --单子来源
					select @sourceKey = source_key from bd_over_time_order where order_id = @order_id;
					--若单子来源是排班加班（@sourceKey=2）,则需要将排班明细表中的is_created_ot = 0
					if @sourceKey=2
						BEGIN
							--将排班明细表中的is_created_ot = 0
							update d
									set is_created_ot = 0
							from bd_kq_schedule_detail d
							INNER JOIN  (select o.ot_date,j.user_serial
								from lr_jiaba j
								INNER JOIN bd_over_time_order o on j.order_id = o.order_id
								where o.order_id = @order_id
							) x on x.user_serial = d.user_serial and x.ot_date = d.shift_date

		END

				END

				-- 准备调用考勤计算

			  declare @us_str_tc varchar(max) = '';
			  declare @date_tc date = null;

        select @us_str_tc += iif(isnull(user_serial,'') = '','',concat(',',user_serial)) from #order_detail_info

        -- 去除最前面的逗号
        set @us_str_tc = iif(left(@us_str_tc,1) = ',',right(@us_str_tc,len(@us_str_tc)-1),@us_str_tc)

        -- 一个加班单的日期都是相同的
        select top 1 @date_tc = date from #order_detail_info

        --计算考勤(3代表加班调用)
        --EXEC bd_proc_timecard @date_tc, @us_str_tc, 3


		--  提交事务
			COMMIT TRAN

	--  结束异常捕捉
		END TRY

	--  异常处理
		BEGIN CATCH
			DECLARE @errStr VARCHAR(MAX) = ERROR_MESSAGE();
			IF XACT_STATE() <> 0
			BEGIN
			  ROLLBACK TRANSACTION;
			END;
			RAISERROR (@errStr, 16, 1);
		END CATCH


--  清除临时表
	drop table #order_detail_info;

	select 1 as result

-- 打开打印受影响行数
SET NOCOUNT OFF;
END;
go
